﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;

namespace ControlExplorer
{
    public class ControlDescription: ISearchable
    {
        public const string IS_FAVORITE_SEARCH_WORD = "favorite";
        
        public ControlDescription(XElement c)
        {
            bool b = false;
            IsFavorite = (c.Attribute("isFavorite") != null && bool.TryParse(c.Attribute("isFavorite").Value, out b) ? b : false);
            AssemblyName = c.Attribute("assembly") != null ? PlatformUtils.AdjustPlatformName(c.Attribute("assembly").Value, false) : string.Empty;
            Name = c.Attribute("name").Value;
            Description = (c.Element("Description") != null) ? c.Element("Description").Value : string.Empty;
            Features = (from f in c.Elements("Feature") select new FeatureDescription(this, f)).ToList();
        }

        public bool IsFavorite { get; set; }
        public string Name { get; set; }
        public string AssemblyName { get; set; }
        public Uri Link
        {
            get
            {
                var first = Features.FirstOrDefault();
                if (first != null)
                    return first.Link;
                else
                    return null;
            }
        }
        public string Description { get; set; }
        public DataTemplate Icon
        {
            get
            {
                return (DataTemplate)App.Current.Resources["IconC1" + Name.Replace(" ", "")] ?? (DataTemplate)App.Current.Resources["IconC1Gray"];
            }
        }
        public DataTemplate IconSearch
        {
            get
            {
                return (DataTemplate)App.Current.Resources["IconSearchC1" + Name.Replace(" ", "")];
            }
        }
        public IEnumerable<FeatureDescription> Features { get; set; }

        #region ISearchable Members

        public bool Contains(string word)
        {
            word = word.ToLower();
            bool inFeature = false;
            foreach (var feature in Features)
            {
                inFeature |= feature.Contains(word);
            }

            return inFeature 
                || Name.ToLower().Contains(word) 
                || AssemblyName.ToLower().Contains(word.ToLower())
         //       || Description.ToLower().Contains(word.ToLower())
                || (IsFavorite && string.Equals(word, IS_FAVORITE_SEARCH_WORD, StringComparison.OrdinalIgnoreCase));
        }

        public bool ContainsAny(string[] searchKeys)
        {
            return searchKeys.Any(key => Contains(key));
        }

        #endregion

        internal IEnumerable<FeatureDescription> GetAllFeatures()
        {
            return Features.SelectMany(f => f.SubFeatures.Count() > 0 ? f.SubFeatures.ToList().Concat(new List<FeatureDescription> {f}) : new List<FeatureDescription> {f});
        }
    }
}
